home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / dld-3_23.lha / dld-3.2.3 / find_exec.c < prev    next >
C/C++ Source or Header  |  1991-05-30  |  2KB  |  91 lines

  1. /* Given a filename, dld_find_executable searches the directories listed in the
  2.    environment variable PATH for a file with that filename.
  3.    A new copy of the complete path name of that file is returned.  This new
  4.    string may be disposed by free() later on.
  5. */
  6.  
  7. /* This file is part of DLD, a dynamic link/unlink editor for C.
  8.    
  9.    Copyright (C) 1990 by W. Wilson Ho.
  10.  
  11.    The author can be reached electronically by how@cs.ucdavis.edu or
  12.    through physical mail at:
  13.  
  14.    W. Wilson Ho
  15.    Division of Computer Science
  16.    University of California at Davis
  17.    Davis, CA 95616
  18.  */
  19.  
  20. /* This program is free software; you can redistribute it and/or modify it
  21.    under the terms of the GNU General Public License as published by the
  22.    Free Software Foundation; either version 1, or (at your option) any
  23.    later version. */
  24.  
  25. #include <sys/file.h>
  26. #include <sys/param.h>
  27. #include <strings.h>
  28.  
  29. #define DEFAULT_PATH ".:~/bin::/usr/local/bin:/usr/new:/usr/ucb:/usr/bin:/bin:/usr/hosts"
  30.  
  31. static char *
  32. copy_of (s)
  33. register char *s;
  34. {
  35.     register char *p = (char *) malloc (strlen(s)+1);
  36.  
  37.     if (!p) return 0;
  38.  
  39.     *p = 0;
  40.     strcpy (p, s);
  41.     return p;
  42. }
  43.  
  44. /* ABSOLUTE_FILENAME_P (fname): True if fname is an absolute filename */
  45. #ifdef atarist
  46. #define ABSOLUTE_FILENAME_P(fname)    ((fname[0] == '/') || \
  47.     (fname[0] && (fname[1] == ':')))
  48. #else
  49. #define ABSOLUTE_FILENAME_P(fname)    (fname[0] == '/')
  50. #endif /* atarist */
  51.  
  52. char *
  53. dld_find_executable (file)
  54. char *file;
  55. {
  56.     char *search;
  57.     register char *p;
  58.     
  59.     if (ABSOLUTE_FILENAME_P(file))
  60.     return copy_of (file);
  61.     
  62.     if (((search = (char *) getenv("DLDPATH")) == 0) &&
  63.     ((search = (char *) getenv("PATH")) == 0))
  64.     search = DEFAULT_PATH;
  65.     
  66.     p = search;
  67.     
  68.     while (*p) {
  69.     char  name[MAXPATHLEN];
  70.     register char *next;
  71.  
  72.     next = name;
  73.     
  74.     /* copy directory name into [name] */
  75.     while (*p && *p != ':') *next++ = *p++;
  76.     *next = 0;
  77.     if (*p) p++;
  78.  
  79.     if (name[0] == '.' && name[1] == 0)
  80.         getwd (name);
  81.     
  82.     strcat (name, "/");
  83.     strcat (name, file);
  84.           
  85.     if (access (name, X_OK) == 0)
  86.         return copy_of (name);
  87.     }
  88.  
  89.     return 0;
  90. }
  91.